package com.trainshop.sms.manage;

import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import net.sf.json.JSONObject;

/**
 * 阿里大鱼短信通道
 */
public class AliSend implements SmsChannelInterface{

	public int sendMessage(TextMessage message){
		ResourceBundle bundle=PropertyResourceBundle.getBundle("sms");
		TaobaoClient client = new DefaultTaobaoClient(bundle.getString("taobaoUrl"), bundle.getString("taobaoAppkey"), bundle.getString("taobaoSecret"));
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("123456");
    	req.setSmsType(bundle.getString("smsType"));
    	req.setSmsFreeSignName(bundle.getString("smsFreeSignName"));
    	req.setSmsParamString(message.getContent());
    	req.setRecNum(message.getPhone());

    	JSONObject jsonObjectCon = JSONObject.fromObject(message.getContent());
    	//选择短信模板
    	/*if(jsonObjectCon.get("template").toString().equals("redgift")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodeRedgift"));
    	}else if(jsonObjectCon.get("template").toString().equals("login")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodeLogin"));
    	}else if(jsonObjectCon.get("template").toString().equals("plan")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodePlan"));
    	}*/
    	if(message.getTemplate().equals("redgift")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodeRedgift"));
    	}else if(message.getTemplate().equals("login")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodeLogin"));
    	}else if(message.getTemplate().equals("plan")){
    		req.setSmsTemplateCode(bundle.getString("smsTemplateCodePlan"));
    	}
    	
    	AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			e.printStackTrace();
		}
    	System.out.println(rsp.getBody());
    	JSONObject jsonObject = JSONObject.fromObject(rsp.getBody());
    	JSONObject jsonObj = JSONObject.fromObject(jsonObject.get("alibaba_aliqin_fc_sms_num_send_response"));
    	JSONObject jsonO = JSONObject.fromObject(jsonObj.get("result"));
    	
    	return Integer.parseInt(jsonO.get("err_code").toString());
	}
}
